package edu.yuhf.dao;

import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import com.yuhf.dbutils.JdbcTemplate;
import com.yuhf.dbutils.ParameterMap;
import com.yuhf.page.Page;
import edu.yuhf.dao.iface.UserDao;
import edu.yuhf.domain.User;
import edu.yuhf.domain.UserMapper;
import lombok.extern.log4j.Log4j;
import lombok.val;

import java.util.*;
import java.util.stream.IntStream;

@Log4j
public class UserJdbcDaoImpl implements UserDao {

    private UserJdbcDaoImpl(){}

    public static UserDao factory(){
        return new UserJdbcDaoImpl();
    }

    @Override
    public List<User> queryAll() {
        String sql="select * from user";
        return JdbcTemplate.query(sql,new UserMapper());
    }

    @Override
    public int login(String userName,String password) {
        String sql = "select count(*) from user where name=? and password=?";
        return JdbcTemplate.getTotalRow(sql,userName,password);
    }

    @Override
    public void queryByPage(Page<User> page, ParameterMap params) {
        List<User> list = null;
        StringBuilder sb = new StringBuilder("select * from user where 1=1 ");
        List<Object> paramList = new ArrayList();
        var map = params.getMap();
        if(map.size()==0){
            sb.append(" limit ?,?");
            list=JdbcTemplate.query(sb.toString(),new UserMapper(),(page.getCurrentPage()-1)*page.getRowNumber(),page.getRowNumber());
        }else{
            map.keySet().forEach(key->{
                if(ParameterMap.NAME_LIKE.equals(key)){
                    sb.append("and ").append(key.substring(0,key.indexOf("_"))).append(" like ?");
                    paramList.add(map.get(key));
                }
                if(ParameterMap.NAME_EQUALS.equals(key)){
                    sb.append("and ").append(key.substring(0,key.indexOf("_"))).append("=?");
                    paramList.add(map.get(key));
                }
            });
            sb.append(" limit ?,?");
            paramList.add((page.getCurrentPage()-1)*page.getRowNumber());
            paramList.add(page.getRowNumber());
            list=JdbcTemplate.query(sb.toString(),new UserMapper(),paramList.toArray());
        }
        page.setPageData(list);
    }

    @Override
    public void getTotalRow(Page<User> page,ParameterMap parameterMap) {
        StringBuilder sb = new StringBuilder("select count(*) from user where 1=1 ");
        List<Object> list = new ArrayList();
        if(null!=parameterMap && parameterMap.getMap().size()!=0){
            var map = parameterMap.getMap();
            map.keySet().forEach(key->{
                if(ParameterMap.NAME_EQUALS.equals(key)){
                    sb.append("and ").append(key.substring(0,key.indexOf("_"))).append("=?");
                    list.add(map.get(key));
                }
                if(ParameterMap.NAME_LIKE.equals(key)){
                    sb.append("and ").append(key.substring(0,key.indexOf("_"))).append(" like ?");
                    list.add(map.get(key));
                }
            });
        }
        log.debug(sb.toString());
        var totalRow = JdbcTemplate.getTotalRow(sb.toString(),list.toArray());
        page.setTotalRow(totalRow);
    }
}
